package com.my12306.common.util.response;

import com.my12306.common.constant.RespConstants;
import com.my12306.common.enums.CodeEnum;
import io.swagger.annotations.ApiModel;
import org.apache.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:苏牧夕
 * @Date:2020/5/13 14:43
 * @Version 1.0
 */
@ApiModel(value = "统一返回结果类", discriminator = "code:响应码,msg:响应的消息,data:返回的数据")
public class ResponseData extends HashMap<String, Object> {
    public ResponseData() {
        put(RespConstants.code, CodeEnum.SUCCESS.getCode());
        put(RespConstants.msg, CodeEnum.SUCCESS.getMessage());
    }

    public static ResponseData fail() {
        return fail(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }

    public static ResponseData error(String msg) {
        return fail(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static ResponseData fail(int code, String msg) {
        ResponseData responseData = new ResponseData();
        responseData.put(RespConstants.msg, msg);
        responseData.put(RespConstants.code, code);
        return responseData;
    }

    public static ResponseData fail(int code, String msg, Object data) {
        ResponseData responseData = new ResponseData();
        responseData.put(RespConstants.msg, msg);
        responseData.put(RespConstants.code, code);
        responseData.put(RespConstants.data, data);
        return responseData;
    }

    public static ResponseData ok(String msg) {
        ResponseData responseData = new ResponseData();
        responseData.put(RespConstants.msg, msg);
        return responseData;
    }

    public static ResponseData ok(int code, String msg) {
        ResponseData responseData = new ResponseData();
        responseData.put(RespConstants.msg, msg);
        responseData.put(RespConstants.code, code);
        return responseData;
    }

    public static ResponseData ok(int code, String msg, Object data) {
        ResponseData responseData = new ResponseData();
        responseData.put(RespConstants.msg, msg);
        responseData.put(RespConstants.code, code);
        responseData.put(RespConstants.data, data);
        return responseData;
    }

    public static ResponseData ok(Map<String, Object> map) {
        ResponseData responseData = new ResponseData();
        responseData.putAll(map);
        return responseData;
    }

    public static ResponseData ok() {
        return new ResponseData();
    }

    public ResponseData put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public Integer getCode() {
        return (Integer) this.get(RespConstants.code);
    }
}
